package com.bigcat.gulimall.demo.utils;

/**
 * @author sufer_yuan
 * @since 2023/7/4
 */

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * Created by shenhongxi on 15/5/27.
 */
public class OffLineAESUtils {

    private static final String PADDING = "AES/ECB/PKCS5Padding";
    private static final String DEFAULT_ENCODING = "utf-8";

    /**
     * 加密
     *
     * @param data 需要加密的内容
     * @param key  加密密码
     * @return
     */
    public static byte[] encrypt(byte[] data, byte[] key) {
        if (key.length != 16) {
            throw new RuntimeException("Invalid AES key length (must be 16 bytes)");
        }
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance(PADDING);// 创建密码器
            cipher.init(Cipher.ENCRYPT_MODE, seckey);// 初始化
            return cipher.doFinal(data);
        } catch (Exception e) {
            throw new RuntimeException("encrypt fail!", e);
        }
    }

    /**
     * 解密
     *
     * @param data 待解密内容
     * @param key  解密密钥
     * @return
     */
    public static byte[] decrypt(byte[] data, byte[] key) {
        if (key.length != 16) {
            throw new RuntimeException("Invalid AES key length (must be 16 bytes)");
        }
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance(PADDING);// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, seckey);// 初始化
            return cipher.doFinal(data);
        } catch (Exception e) {
            throw new RuntimeException("decrypt fail!", e);
        }
    }

    public static String encryptToBase64(String data, String key) {
        try {
            byte[] valueByte = encrypt(data.getBytes(DEFAULT_ENCODING), key.getBytes(DEFAULT_ENCODING));
            return new Base64().encodeToString(valueByte);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("encrypt fail!", e);
        }
    }

    /**
     * 加密
     */
    public static String encrypt(String value, String salt) {
        return OffLineAESUtils.encryptToBase64(value, String.format("%-16s", salt));
    }

    /**
     * 解密
     */
    public static String decrypt(String valueEncrypt, String salt) {
        return OffLineAESUtils.decryptFromBase64(valueEncrypt, String.format("%-16s", salt));
    }

    public static String decryptFromBase64(String data, String key) {
        try {
            byte[] originalData = new Base64().decode(data.getBytes());
            byte[] valueByte = decrypt(originalData, key.getBytes(DEFAULT_ENCODING));
            return new String(valueByte, DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("decrypt fail!", e);
        }
    }

    public static void main(String[] args) {
        //加密
        System.out.println(encrypt("119.818922,28.900078", "e1780b101e0e075c"));
        System.out.println(encrypt("0", "e1780b101e0e075c"));
        System.out.println(CONTEXT_PARAM.CUR_TENANTID.toString());
        // 解密
        System.out.println(decrypt("jGhR4hbLaLGk9NRSmmkjUg==", "e1780b101e0e075c"));
        System.out.println("123456".length());
    }
}